//
//  PurchasingAnalysisViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/8/21.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PurchasingAnalysisViewController: MarketingAnalysisViewController {
    
    @IBAction override func tenderAction(_ sender: UIButton) {
        
        let destinate = ProjectDetailChartViewController()
        
        destinate.selectedDate = self.father!.endDate()
        
        destinate.title = self.lbls[0].text
        
        destinate.ifcaigou = true
        
        destinate.candetail = true
        
        destinate.companys = self.companys
        
        destinate.selectedCompany = self.selectedCompany
        
        destinate.detailPath = PurchaseStastic
        
        destinate.key = "PurchaseApply"
        
        self.push(destination: destinate)
        
//        if User.canEnter {
//            let destinate = ProjectDetailChartViewController()
//
//            destinate.title = self.lbls[0].text
//
//            destinate.companys = self.companys
//
//            destinate.selectedCompany = self.selectedCompany
//
//            destinate.detailPath = PurchaseStastic
//
//            destinate.key = "PurchaseApply"
//
//            self.push(destination: destinate)
//        } else {
//            self.alert("您无此权限")
//        }
        
    }
    
    @IBAction override func saleAction(_ sender: UIButton) {
        let destinate = ProjectDetailChartViewController()
        
        destinate.selectedDate = self.father!.endDate()
        
        destinate.title = self.lbls[1].text
        
        destinate.ifcaigou = true
        
        destinate.companys = self.companys
        
        destinate.selectedCompany = self.selectedCompany
        
        destinate.detailPath = PurchaseStastic
        
        destinate.key = "PurchaseContract"
        
        self.push(destination: destinate)

        
    }
    
    @IBAction override func tickAction(_ sender: UIButton) {
        
        let destinate = ProjectDetailChartViewController()
        
        destinate.selectedDate = self.father!.endDate()
        
        destinate.title = self.lbls[2].text
        
        destinate.companys = self.companys
        
        destinate.detailPath = PurchaseStastic
        
        destinate.selectedCompany = self.selectedCompany
        
        destinate.ifcaigou = true
        
        destinate.key = "Invoice"
        
        self.push(destination: destinate)
        
    }
    
    @IBAction override func ticketBackAction(_ sender: UIButton) {
        let destinate = ProjectDetailChartViewController()
        
        destinate.selectedDate = self.father!.endDate()
        
        destinate.title = self.lbls[3].text
        
        destinate.ifcaigou = true
        
        destinate.companys = self.companys
        
        destinate.selectedCompany = self.selectedCompany
        
        destinate.detailPath = PurchaseStastic
        
        destinate.key = "PaymentInformation"
        
        self.push(destination: destinate)
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    var contents: [String: JSON]?

    override func path(for name: String) -> String {
        if name == "GetOrg" {
            return GETORG
        } else {
            return PurchasingManageStastic
        }
    }
    
    override func success(for name: String) -> NetSuccess {
        func getOrgSuc(json: JSON)->Void{
            if json.contents?["success"]?.bool == true {
                if let data = json.contents?["data"]?.array {
                    companys = data
                    if companys.count > 0 {
                        let item = companys[0]
                        self.companyField.text = item.contents?["name"]?.rawString()
                        selectedCompany = item.contents!["code"]!.rawString()!
                        load(with: "Detail")
                    }
                }
            } else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        
        func detail(json: JSON)->Void{
            
            if json.contents?["success"]?.bool == true {
                if let data = json.contents?["data"]?.contents {
                    
                    self.contents = data
                    let first = data["PurchaseApply"]?.contents?["yearCount"]?.int ?? 0
                    self.lbls[0].text = data["PurchaseApply"]?.contents?["typeTitle"]?.rawString()

                    let firstMonth = data["PurchaseApply"]?.contents?["monthCount"]?.int ?? 0

                    let second = data["PurchaseContract"]?.contents?["yearCount"]?.int ?? 0
                    self.lbls[1].text = data["PurchaseContract"]?.contents?["typeTitle"]?.rawString()
                    let secondMonth = data["PurchaseContract"]?.contents?["monthCount"]?.int ?? 0
                    
                    
                    let third = data["Invoice"]?.contents?["yearCount"]?.int ?? 0
                    print(third)
                    self.lbls[2].text = data["Invoice"]?.contents?["typeTitle"]?.rawString()


                    let thirdMonth = data["Invoice"]?.contents?["monthCount"]?.int ?? 0

                    let forth = data["PaymentInformation"]?.contents?["yearCount"]?.int ?? 0
                    self.lbls[3].text = data["PaymentInformation"]?.contents?["typeTitle"]?.rawString()

                    let forthMonth = data["PaymentInformation"]?.contents?["monthCount"]?.int ?? 0
                    self.targetAmount.text = String(format: "%i", first / 1000000).rmbformat
                    self.firstLabel.text = "当月" + String(format: "%i", firstMonth / 1000000).rmbformat + "万元"
                    self.saleAmount.text = String(format: "%i", second / 1000000).rmbformat
                    self.secondLabel.text =  "当月" + String(format: "%i", secondMonth / 1000000).rmbformat + "万元";
                    self.contractAmount.text = String(third / 1000000).rmbformat
                    self.thirdLabel.text = "当月" + String(format: "%i", thirdMonth / 1000000).rmbformat + "万元"

                    self.contractBackAmount.text = String(format: "%i", forth / 1000000).rmbformat

                    self.forthLabel.text = "当月" + String(format: "%i", forthMonth / 1000000).rmbformat + "万元"
                }
            }else {
                self.alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        
        if name == "GetOrg" {
            return getOrgSuc
        } else {
            return detail
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
